<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>和相同的二元子数组</title>
</head>
<body>
  <script>
    function fn(nums, goal) {
      const n = nums.length
      const map = new Map()
      let preSum = 0
      let ret = 0
      map.set(0, 1)
      for (let i = 0; i < n; i++) {
        preSum += nums[i]
        ret += (map.get(preSum - goal) || 0)
        map.set(preSum, (map.get(preSum) || 0) + 1)
      }
      return ret
    }
    console.log(fn([1, 0, 1, 0, 1], 2))
  </script>
</body>
</html>